package com.per.yyyFrame.controller;

import com.per.yyyFrame.common.CheckParam;
import com.per.yyyFrame.common.InvocationHandler;
import com.per.yyyFrame.common.ServerResponse;
import com.per.yyyFrame.config.BizExceptional;
import com.per.yyyFrame.enums.SqlType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/2/17
 * Time: 15:39
 * Description:
 */
@RestController
@RequestMapping("yyy/JdbcController/")
public class JdbcController {

    @Autowired
    InvocationHandler invocationHandler;


    @GetMapping("testInsert")
    public ServerResponse testInsert(@RequestParam Map map) throws SQLException {
        CheckParam.checkInit(map)
                .param("id").checkCondition("用户id必传")
                .param("type").checkCondition("操作类型必传")
                .verify();

        SqlType sqlType = SqlType.getName(map.get("type").toString());
        switch (sqlType) {
            case SELECT:
                return new ServerResponse(200,  invocationHandler.execute("select username,password from test where id=?",
                        new ArrayList(){{add("username");add("password");}},
                        new ArrayList() {{
                            add(map.get("id"));
                        }}));
            case DELETE:
                return new ServerResponse(200, invocationHandler.execute("delete from test where id=?",
                        new ArrayList(),
                        new ArrayList() {{
                            add(map.get("id"));
                        }}));
            case UPDATE:
                return new ServerResponse(200, invocationHandler.execute("update test set username=? ,password=? where id=?",
                        new ArrayList(),
                        new ArrayList() {{
                            add(map.get("username"));
                            add(map.get("password"));
                            add(map.get("id"));
                        }}));
            case INSERT:
                return new ServerResponse(200, invocationHandler.execute("insert into test(username,password)values(?,?)",
                        new ArrayList(),
                        new ArrayList() {{
                            add(map.get("username"));
                            add(map.get("password"));
                        }}));
            default:
                throw new BizExceptional("无该类型操作");

        }
        //return null;
    }
}
